package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.DtPictureAuditVo;
import com.xueyi.aixihu.entity.vo.DtPictureCountVo;
import com.xueyi.aixihu.reqeust.DtFindByAuditPictureReq;
import com.xueyi.aixihu.reqeust.DtUpdatePictureReq;
import com.xueyi.aixihu.reqeust.SendPictureAndDescribeReq;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author yuyu
 * @date : 2022/8/16
 * @Description : 图片管理
 */
public interface DtPictureMessageMapper {
    /**
     * 查询图片审核的集合
     *
     * @param dtFindByAuditPictureReq
     * @param userAddressId
     * @return
     */
    @Select("<script>" +
            " SELECT * FROM dt_advert WHERE `status` = #{dtFindByAuditPictureReq.status}  " +
            " <if test = 'dtFindByAuditPictureReq.isValid != null' > " +
            " and is_valid = #{dtFindByAuditPictureReq.isValid} " +
            " </if> " +
            " <if test = 'userAddressId != null' > " +
            "  and from_to_area_id in (${userAddressId}) " +
            " </if> " +
            "  order by create_time desc " +
            " limit #{dtFindByAuditPictureReq.startIndex},#{dtFindByAuditPictureReq.limit} " +
            "</script>")
    public List<DtPictureAuditVo> findByWaitAuditPicture(@Param("dtFindByAuditPictureReq") DtFindByAuditPictureReq dtFindByAuditPictureReq, @Param("userAddressId") String userAddressId);

    /**
     * 查询图片审核中条数
     *
     * @param dtFindByAuditPictureReq
     * @param userAddressId
     * @return
     */
    @Select("<script>" +
            " select count(da.id) " +
            " from dt_advert da where da.status = " +
            " #{dtFindByAuditPictureReq.status}" +
            " <if test = 'userAddressId != null' > " +
            "  and da.from_to_area_id in (${userAddressId}) " +
            " </if> " +
            " <if test = 'dtFindByAuditPictureReq.isValid != null' > " +
            " and da.is_valid = #{dtFindByAuditPictureReq.isValid} " +
            " </if> " +
            "</script>")
    public Integer findByWaitAuditPictureCount(@Param("dtFindByAuditPictureReq") DtFindByAuditPictureReq dtFindByAuditPictureReq, @Param("userAddressId") String userAddressId);

    /**
     * 修改图片状态
     *
     * @param dtUpdatePictureReq
     */
    @Update("<script>" +
            " update dt_advert set " +
            " <if test = 'dtUpdatePictureReq.status != null ' > " +
            " status = #{dtUpdatePictureReq.status} " +
            " </if>" +
            " <if test = 'dtUpdatePictureReq.isValid != null ' > " +
            " is_valid = #{dtUpdatePictureReq.isValid} " +
            " </if>" +
            " where id in (${dtUpdatePictureReq.id}) " +
            "</script>")
    public void updateIsValid(@Param("dtUpdatePictureReq") DtUpdatePictureReq dtUpdatePictureReq);

    /**
     * 查询单条图片数据
     *
     * @param id
     * @return
     */
    @Select(" select * from dt_advert where id = #{id} ")
    public DtPictureAuditVo findByOnePicture(Integer id);

    /**
     * 查询已通过跟未通过的数量
     *
     * @return
     */
    @Select(" SELECT " +
            " SUM(IF(status = 1,1,NULL)) alreadyTotal, " +
            " SUM(IF(status = 0,1,NULL)) waitTotal  " +
            " FROM " +
            " dt_advert ")
    public DtPictureCountVo findByPictureCount();


    /**
     * 发送描述和图片
     *
     * @param sendPictureAndDescribeReq
     */
    @Insert(" INSERT INTO dt_advert (thumb,content,create_time,is_valid,sort,cid) " +
            " VALUES (#{image},#{content},#{createTime},1,0,10) ")
    void insertPictureAndDescribe(SendPictureAndDescribeReq sendPictureAndDescribeReq);

}
